package com.ihd.ihardware.common.utils;

import com.ihd.ihardware.pojo.CurrStatus;

import cn.wowjoy.commonlibrary.utils.HexUtils;

import static cn.wowjoy.commonlibrary.utils.HexUtils.byteArrayToInt;
import static cn.wowjoy.commonlibrary.utils.HexUtils.byteToBinaryString;
import static cn.wowjoy.commonlibrary.utils.HexUtils.byteToInt;
import static cn.wowjoy.commonlibrary.utils.HexUtils.hexStringToBytes;

public class DataUtils {
    public static String getName(byte[] data) {
        if (data.length < 9)
            return "";
        else
            return String.valueOf((char) data[5]) + String.valueOf((char) data[6]) + String.valueOf((char) data[7]) + String.valueOf((char) data[8]);
    }

    public static String getOKOKName(byte[] data) {
        if (data.length < 8)
            return "";
        else
            return String.valueOf((char) data[5]) + String.valueOf((char) data[6]) + String.valueOf((char) data[7]);
    }

    public static boolean isValidDevice(byte[] data) {
        return "VCHM".equals(getName(data)) || "ADV".equals(getOKOKName(data));
    }

    public static boolean isStable(byte[] data) {
        if (data.length < 19)
            return false;
        String tag = "";
        if ("VCHM".equals(getName(data)))
            tag = byteToBinaryString(data[17]);
        else
            tag = byteToBinaryString(data[18]);
        if ("1".equals(tag.substring(7, 8)))
            return true;
        else
            return false;
    }

    public static String getWeight(byte[] data) {
        if (data.length < 22)
            return "";
        byte[] weight;
        if ("VCHM".equals(getName(data)))
            weight = new byte[]{data[19], data[20]};
        else
            weight = new byte[]{data[20], data[21]};
        return byteArrayToInt(weight) / 100 + "." + byteArrayToInt(weight) % 100;
    }

    public static String getMacAddress(byte[] data) {
        byte[] mac;
        if ("VCHM".equals(getName(data)))
            mac = new byte[]{data[23], data[24], data[25], data[26], data[27], data[28]};
        else
            mac = new byte[]{data[25], data[26], data[27], data[28], data[29], data[30]};
        return HexUtils.bytesToHex(mac);
    }

    public static CurrStatus parseStatus(String com) {
        CurrStatus cs = new CurrStatus();
        cs.setBattery(byteToInt(hexStringToBytes(com.substring(6, 8))[0]));
        cs.setSpeed(byteArrayToInt(hexStringToBytes(com.substring(8, 12))) / (float) 10);
        cs.setTotalway(byteArrayToInt(hexStringToBytes(com.substring(12, 16))) / (double) 10);
        cs.setCurrway(byteArrayToInt(hexStringToBytes(com.substring(16, 20))) / (double) 10);
        cs.setRidetime(byteArrayToInt(hexStringToBytes(com.substring(20, 24))));

        String status = byteToBinaryString(hexStringToBytes(com.substring(24, 26))[0]);
        cs.setLock(Integer.parseInt(status.substring(0, 1)));
        cs.setCharge(Integer.parseInt(status.substring(1, 2)));
        cs.setLonoff(Integer.parseInt(status.substring(2, 3)));
        cs.setLauto(Integer.parseInt(status.substring(3, 4)));
        cs.setMode(Integer.parseInt(status.substring(4, 5)));
        cs.setXh(Integer.parseInt(status.substring(5, 6)));
        if (Integer.parseInt(status.substring(6, 8)) == 0)
            cs.setLevel(0);
        else if (Integer.parseInt(status.substring(6, 8)) == 1)
            cs.setLevel(1);
        else if (Integer.parseInt(status.substring(6, 8)) == 10)
            cs.setLevel(2);
        else if (Integer.parseInt(status.substring(6, 8)) == 11)
            cs.setLevel(3);

        String err = byteToBinaryString(hexStringToBytes(com.substring(26, 28))[0]);
        cs.setErr("");
        if (err.substring(0, 1).equals("1"))
            cs.setErr("其它故障");
        else if (err.substring(1, 2).equals("1"))
            cs.setErr("断电刹把启动");
        else if (err.substring(2, 3).equals("1"))
            cs.setErr("电机缺相故障");
        else if (err.substring(3, 4).equals("1"))
            cs.setErr("欠压保护");
        else if (err.substring(4, 5).equals("1"))
            cs.setErr("控制器故障");
        else if (err.substring(5, 6).equals("1"))
            cs.setErr("转把故障");
        else if (err.substring(6, 7).equals("1"))
            cs.setErr("霍尔故障");
        else if (err.substring(7, 8).equals("1"))
            cs.setErr("与控制器通讯故障");
        return cs;
    }
}
